/*
 "http://ravioli-2:8080/addama-rest/dudley-sample/path/datasource/DO/search?FREE_TEXT=DO1
 &PROJECTION=oligoName&PROJECTION=notes&PROJECTION=gene&PROJECTION=concentration"
 */
function getOligoSearchUrl(oligoType, freeSearchOp, matchingAllTerms, matchingSearchHash) {
    var searchOligoUrl = "/addama-rest/dudley-sample/path/datasource/" + oligoType + "/search?" + freeSearchOp + matchingAllTerms +
                         "&PROJECTION=oligoName" +
                         "&PROJECTION=notes" +
                         "&PROJECTION=gene" +
                         "&PROJECTION=sequence" +
                         "&PROJECTION=yName" +
                         "&PROJECTION=project" +
                         "&PROJECTION=modification" +
                         "&PROJECTION=purification" +
                         "&PROJECTION=concentration" +
                         "&PROJECTION=orderedBy" +
                         "&PROJECTION=comments" +
                         matchingSearchHash;
    return searchOligoUrl;
}

function getSolrDOligoUrl(searchTerm, maxresults) {
    return "/solr/select/?q=" + searchTerm + "&wt=json&rows=" + maxresults 
    + "&fl=doligoKey,doligoName,doligoNotes,doligoGene,doligoSequence,doligoYName,doligoProject,doligoModification,doligoPurification,doligoConcentration,doligoOrderedBy,doligoComments";
}

function getSolrLOligoUrl(searchTerm, maxresults) {
    return "/solr/select/?q=" + searchTerm + "&wt=json&rows=" + maxresults 
    + "&fl=loligoKey,loligoName,loligoNotes,loligoGene,loligoSequence,loligoYName,loligoProject,loligoModification,loligoPurification,loligoConcentration,loligoOrderedBy,loligoComments";
}

function getOligoPagingColumns() {
    var opac = [
        {
            name: 'indexNum',
            type: 'float'
        },
        {
            name: 'key'
        },
        {
            name: 'oligoName'
        },
        {
            name: 'notes'
        },
        {
            name: 'gene'
        },
        {
            name: 'sequence'
        },
        {
            name: 'yName'
        },
        {
            name: 'project'
        },
        {
            name: 'modification'
        },
        {
            name: 'purification'
        },
        {
            name: 'concentration'
        },
        {
            name: 'orderedBy'
        },
        {
            name: 'comments'
        }
    ];
    return opac;
}

function translateOligoResultToJson(jsonSearchArray) {
    var jsonObj = [];
    var oligoKey = jsonSearchArray.name;
    jsonObj[0] = oligoKey.substring(2, oligoKey.length);
    jsonObj[1] = jsonSearchArray.name;
    jsonObj[2] = jsonSearchArray.oligoName;
    jsonObj[3] = jsonSearchArray.notes;
    jsonObj[4] = jsonSearchArray.gene;
    jsonObj[5] = jsonSearchArray.sequence;
    jsonObj[6] = jsonSearchArray.yName;
    jsonObj[7] = jsonSearchArray.project;
    jsonObj[8] = jsonSearchArray.modification;
    jsonObj[9] = jsonSearchArray.purification;
    jsonObj[10] = jsonSearchArray.concentration;
    jsonObj[11] = jsonSearchArray.orderedBy;
    jsonObj[12] = jsonSearchArray.comments;
    var obsolete = jsonSearchArray.deletedOligo;
    if (obsolete != undefined) {
        jsonObj[0] = jsonSearchArray.name;
        jsonObj[1] = jsonSearchArray.oligoName;
        jsonObj[2] = "Record Deleted, contact Cathy/Jake to recover " + jsonSearchArray.name;
        jsonObj[3] = "Record Deleted";
        jsonObj[4] = "";//jsonSearchArray.strainSource;
        jsonObj[5] = "";//jsonSearchArray.strainParents;
        jsonObj[6] = "";//jsonSearchArray.boxNumber;
        jsonObj[7] = "";//jsonSearchArray.dateFrozen;
        jsonObj[8] = "";//jsonSearchArray.contributor;
        jsonObj[9] = "";//jsonSearchArray.proteomics;
    }
    return jsonObj;
}

var oligoSolrTranslator = function(jsonSearchArray){
    var jsonObj = [];
    var oligoKey = jsonSearchArray.doligoKey;
    jsonObj[0] = oligoKey.substring(2, oligoKey.length);
    jsonObj[1] = oligoKey;
    jsonObj[2] = jsonSearchArray.doligoName;
    jsonObj[3] = jsonSearchArray.doligoNotes;
    jsonObj[4] = jsonSearchArray.doligoGene;
    jsonObj[5] = jsonSearchArray.doligoSequence;
    jsonObj[6] = jsonSearchArray.doligoYName;
    jsonObj[7] = jsonSearchArray.doligoProject;
    jsonObj[8] = jsonSearchArray.doligoModification;
    jsonObj[9] = jsonSearchArray.doligoPurification;
    jsonObj[10] = jsonSearchArray.doligoConcentration;
    jsonObj[11] = jsonSearchArray.doligoOrderedBy;
    jsonObj[12] = jsonSearchArray.doligoComments;
    return jsonObj;
};

var laboligoSolrTranslator = function(jsonSearchArray){
    var jsonObj = [];
    var oligoKey = jsonSearchArray.loligoKey;
    jsonObj[0] = oligoKey.substring(2, oligoKey.length);
    jsonObj[1] = oligoKey;
    jsonObj[2] = jsonSearchArray.loligoName;
    jsonObj[3] = jsonSearchArray.loligoNotes;
    jsonObj[4] = jsonSearchArray.loligoGene;
    jsonObj[5] = jsonSearchArray.loligoSequence;
    jsonObj[6] = jsonSearchArray.loligoYName;
    jsonObj[7] = jsonSearchArray.loligoProject;
    jsonObj[8] = jsonSearchArray.loligoModification;
    jsonObj[9] = jsonSearchArray.loligoPurification;
    jsonObj[10] = jsonSearchArray.loligoConcentration;
    jsonObj[11] = jsonSearchArray.loligoOrderedBy;
    jsonObj[12] = jsonSearchArray.loligoComments;
    return jsonObj;
};

function initOligoJson(oligoHash) {
    oligoHash.oligoName = undefined;
    oligoHash.notes = undefined;
    oligoHash.gene = undefined;
    oligoHash.concentration = undefined;

}

function getOligoSearchOptionsArray() {
    return [
        ['all', 'All'],
        ['oligoNum', 'oligoKey'],
        ['oligoName', 'oligoName'],
        ['yName', 'yName'],
        ['notes', 'notes'],
        ['gene', 'gene'],
        ['sequence', 'sequence'],
        ['project', 'project'],
        ['modification', 'modification'],
        ['purification', 'purification'],
        ['concentration','concentration'],
        ['orderedBy','orderedBy']
    ];
}

function getOligoSearchColumnHeaders() {
    var oligoColumns = [
        {
            header: 'Index',
            width: 50,
            sortable: true,
            dataIndex: 'indexNum'
        },
        {
            id:'DOkey',
            header: 'OligoKey',
            minColumnWidth: 100,
            sortable: true,
            dataIndex: 'key'
        },
        {
            header: 'Name',
            width: 100,
            sortable: true,
            dataIndex: 'oligoName'
        },
        {
            header: 'Notes',
            width: 250,
            sortable: true,
            dataIndex: 'notes'
        },
        {
            header: 'Gene',
            width: 100,
            sortable: true,
            dataIndex: 'gene'
        },
        {
            header: 'Sequence',
            width: 300,
            sortable: true,
            dataIndex: 'sequence'
        },
        {
            header: 'YName',
            hidden: true,
            dataIndex: 'yName'
        },
        {
            header: 'Project',
            hidden: true,
            dataIndex: 'project'
        },
        {
            header: 'Modification',
            hidden: true,
            dataIndex: 'modification'
        },
        {
            header: 'Purification',
            hidden: true,
            dataIndex: 'purification'
        },
        {
            header: 'Concentration',
            hidden: true,
            dataIndex: 'concentration'
        }

    ];
    return oligoColumns;
}

function populateOligoJson(oHash, g) {
    oHash.name = g.getSelectionModel().getSelected().data.key;
    oHash.oligoName = g.getSelectionModel().getSelected().data.oligoName;
    oHash.yName = g.getSelectionModel().getSelected().data.yName;
    oHash.notes = g.getSelectionModel().getSelected().data.notes;
    oHash.sequence = g.getSelectionModel().getSelected().data.sequence;
    oHash.gene = g.getSelectionModel().getSelected().data.gene;
    oHash.project = g.getSelectionModel().getSelected().data.project;
    oHash.modification = g.getSelectionModel().getSelected().data.modification;
    oHash.purification = g.getSelectionModel().getSelected().data.purification;
    oHash.concentration = g.getSelectionModel().getSelected().data.concentration;
    oHash.orderedBy = g.getSelectionModel().getSelected().data.orderedBy;
    oHash.comments = g.getSelectionModel().getSelected().data.comments;
}

function populateSingleDeletionOligoViaSearch(oHash, rec) {
    /*
     jsonObj[0] = oligoKey.substring(2, oligoKey.length);
     jsonObj[1] = jsonSearchArray.name;
     jsonObj[2] = jsonSearchArray.oligoName;
     jsonObj[3] = jsonSearchArray.notes;
     jsonObj[4] = jsonSearchArray.gene;
     jsonObj[5] = jsonSearchArray.sequence;
     jsonObj[6] = jsonSearchArray.yName;
     jsonObj[7] = jsonSearchArray.project;
     jsonObj[8] = jsonSearchArray.modification;
     jsonObj[9] = jsonSearchArray.purification;
     jsonObj[10] = jsonSearchArray.concentration;
     jsonObj[11] = jsonSearchArray.orderedBy;
     */
    oHash.name = rec[1];
    oHash.oligoName = rec[2];
    oHash.yName = rec[6];
    oHash.notes = rec[3];
    oHash.sequence = rec[5];
    oHash.gene = rec[4];
    oHash.project = rec[7];
    oHash.modification = rec[8];
    oHash.purification = rec[9];
    oHash.concentration = rec[10];
    oHash.orderedBy = rec[11];
    oHash.comments = rec[12];
}


function validateCreateUpdateOligo(oType) {
    /*
     if (oType == 'DO') {
     if (Ext.getCmp("doligoNameId").getValue() == null || Ext.getCmp("doligoNameId").getValue() == "") {
     Ext.Msg.alert('Error', 'Oligo name is required.');
     return false;
     } else if (Ext.getCmp("doligoGeneId").getValue() == null || Ext.getCmp("doligoGeneId").getValue() == "") {
     Ext.Msg.alert('Error', 'Gene is required.');
     return false;
     } else if (Ext.getCmp("doligoConcentrationId").getValue() == null || Ext.getCmp("doligoConcentrationId").getValue() == "") {
     Ext.Msg.alert('Error', 'Concentration is required');
     return false;
     } else  if (Ext.getCmp("doligoYNameId").getValue() == null || Ext.getCmp("doligoYNameId").getValue() == "") {
     Ext.Msg.alert('Error', 'Y Name is required.');
     return false;
     } else if (Ext.getCmp("doligoNotesId").getValue() == null || Ext.getCmp("doligoNotesId").getValue() == "") {
     Ext.Msg.alert('Error', 'Oligo Notes is required.');
     return false;
     }
     }

     if (oType == 'AO') {
     if (Ext.getCmp("aoligoNameId").getValue() == null || Ext.getCmp("aoligoNameId").getValue() == "") {
     Ext.Msg.alert('Error', 'Oligo name is required.');
     return false;
     //} //else if (Ext.getCmp("aoligoGeneId").getValue() == null || Ext.getCmp("aoligoGeneId").getValue() == "") {
     // Ext.Msg.alert('Error', 'Gene is required.');
     // return false;
     } else if (Ext.getCmp("aoligoConcentrationId").getValue() == null || Ext.getCmp("aoligoConcentrationId").getValue() == "") {
     Ext.Msg.alert('Error', 'Concentration is required');
     return false;
     } else  if (Ext.getCmp("aoligoYNameId").getValue() == null || Ext.getCmp("aoligoYNameId").getValue() == "") {
     Ext.Msg.alert('Error', 'Y Name is required.');
     return false;
     } else if (Ext.getCmp("aoligoNotesId").getValue() == null || Ext.getCmp("aoligoNotesId").getValue() == "") {
     Ext.Msg.alert('Error', 'Oligo Notes is required.');
     return false;
     }

     }*/

    return true;
}

function exportOligos(oligodata, label) {
    var csvText = "";
    oligodata.sort(function(a, b) {
        return a[0] - b[0];
    });
    csvText = label + "Key," + label + "Name," + label + "Notes," + label + "Gene," + label + "Sequence," + label + "YName," + label + "Project," + label + "Modification," + label + "Purification," + label + "Concentration," + label + "OrderedBy," + label + "Comments" + breakDelimiter;
    for (var c = 0; c < oligodata.length; c++) {
        var cArray = oligodata[c];
        for (var ci = 0; ci < cArray.length; ci++) {
            if (cArray[ci] == undefined || cArray[ci] == null) {
                cArray[ci] = "";
            }
            cArray[ci] = cArray[ci] + "";
            cArray[ci] = cArray[ci].replace(/'/g, "");
            cArray[ci] = '"' + cArray[ci] + '"';
        }
        csvText = csvText + "" + cArray[1] + "," + cArray[2] + "," + cArray[3] + "," + cArray[4] + "," + cArray[5] + "," + cArray[6] + "," + cArray[7] + ","
                + cArray[8] + "," + cArray[9] + "," + cArray[10] + "," + cArray[11] + "," + cArray[12] + breakDelimiter;
    }

    export2Csv(label + "SearchedResults.csv", csvText, label + "2Csv");
}


